package com.scenic.userservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scenic.common.bean.User;
import com.scenic.common.exception.ScenicSpotException;
import com.scenic.common.service.UserService;
import com.scenic.userservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public User login(String username, String password) throws ScenicSpotException {
        User user = userMapper.selectByUsername(username);
        //根据用户名查询是否存在
        if (user == null) {
            throw new ScenicSpotException("该用户不存在，无法登录");
        }
        //判断用户的状态是否禁用或者注销
        if (user.getStatus().equals(2) || user.getStatus().equals(3)) {
            throw new ScenicSpotException("该用户被禁用或者注销，无法登录");
        }
        //对输入的密码加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + user.getSalt()));
        //和数据库的密码进行对比
        if (!md5Pwd.equals(user.getPassword())) {
            throw new ScenicSpotException("输入的密码不一致");
        }
        return user;
    }

    @Override
    public Boolean reg(User user) throws ScenicSpotException {
        //判断被注册的用户名是否存在
        if (userMapper.selectByUsername(user.getUsername()) != null) {
            throw new ScenicSpotException("该用户已经存在无法注册！");
        }
        //生成盐
        String salt = RandomUtil.randomString(8);
        user.setSalt(salt);
        //密码加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(user.getPassword() + salt));
        user.setPassword(md5Pwd);

        //添加用户
        return userMapper.insert(user) == 1;
    }
    @Override
    public Boolean delete(Integer id) throws ScenicSpotException {
        return userMapper.delete(id) == 1;

    }

    @Override
    public Boolean update(User user) throws ScenicSpotException {
        List<User> userList = userMapper.selectByCondition(user);
        //用户名不能相同
        if (userList.stream()
                .anyMatch(item -> item.getUsername().equals(user.getUsername()) && !item.getId().equals(user.getId()))) {
            throw new ScenicSpotException("用户名相同不能修改");
        }
        /**
         * 判断是否为已经认证的用户
         * 真实姓名不能为空
         * 身份证不能为空
         * 手机号不能为空
         * */
        if (!user.getIdCard().isEmpty() && !user.getRealname().isEmpty() && !user.getPhone().isEmpty()) {
            user.setStatus(1);
        }else {
            user.setStatus(0);
        }
        return userMapper.update(user) == 1;
    }

    @Override
    public Boolean chgPwd(String newPwd, String oldPwd, String confirmPwd, Integer id) throws ScenicSpotException {
        //取出盐
        User u = userMapper.selectById(id);
        String salt = u.getSalt();
        //判断两次密码是否一致
        if (!newPwd.equals(confirmPwd)) {
            throw new ScenicSpotException("两次密码不一致！");
        }
        //判断原密码是否正确
        String inputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPwd + salt));
        if (!inputOldPwd.equals(u.getPassword())) {
            throw new ScenicSpotException("原密码不正确！");
        }
        //设置新密码
        String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPwd + salt));
        User user = new User();
        user.setId(id);
        user.setPassword(md5NewPwd);
        return userMapper.chgPwdAndPayPwd(user) == 1;
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }


    @Override
    public PageInfo<User> selectByCondition(User condition, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<User> user = userMapper.selectByCondition(condition);
        PageInfo<User> pageInfo = new PageInfo<>(user);
        return pageInfo;
    }

    @Override
    public boolean chgPayPwd(String oldPassword, String oldPayPwd, String newPayPwd, String confirmPayPwd, Integer id) throws ScenicSpotException {
        //取出盐
        User u = userMapper.selectById(id);
        String salt = u.getSalt();
        //判断两次密码是否一致
        if (!newPayPwd.equals(confirmPayPwd)) {
            throw new ScenicSpotException("两次密码不一致！");
        }
        //判断原密码是否正确
        String inputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPassword + salt));
        if (!inputOldPwd.equals(u.getPassword())) {
            throw new ScenicSpotException("原密码不正确！");
        }
        //判断支付密码是否正确
        String inputOldPayPwd = SecureUtil.md5(SecureUtil.md5(oldPayPwd + salt));
        if (!inputOldPayPwd.equals(u.getPayPassword())) {
            throw new ScenicSpotException("原支付密码不正确！");
        }
        //设置新密码
        String md5NewPayPwd = SecureUtil.md5(SecureUtil.md5(newPayPwd + salt));
        User user = new User();
        user.setId(id);
        user.setPayPassword(md5NewPayPwd);
        return userMapper.chgPwdAndPayPwd(user) == 1;
    }

    @Override
    public List<Map<String, Object>> userTop5() {
        return userMapper.userTop5();
    }

    @Override
    public void recharge(User user, Integer userId) throws ScenicSpotException {
        //查询该用户是否属于该用户
        User u = userMapper.selectById(userId);
        if (!u.getUsername().equals(user.getUsername())) {
            throw new ScenicSpotException("不属于该用户无法充值！");
        }
        //设置账户金额
        //user.setMoney(u.getMoney().add(user.getMoney()));
        //充值业务
        if (userMapper.update(user) != 1) {
            throw new ScenicSpotException("充值失败");
        }
    }
}
